﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using gfLibrary;
using DBUtility;
using Model;
using BLL;

namespace GreatGF
{
    public partial class CONST_DEFINE_WEB_CATEGORY_Insert_Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             if (Page.IsPostBack == false)
            {

                string strMode = Request["m"];

                if((strMode == "u")||(strMode == "b")||(strMode == "r"))
                {
                    string strId = Request["UniqueKey"];

                    CONST_DEFINE_WEB_CATEGORY_BLL m_CONST_DEFINE_WEB_CATEGORY_BLL = new CONST_DEFINE_WEB_CATEGORY_BLL();

                    if(m_CONST_DEFINE_WEB_CATEGORY_BLL.Exists(Convert.ToInt32(strId)) == true)
                    {
                        CONST_DEFINE_WEB_CATEGORY m_CONST_DEFINE_WEB_CATEGORY = new CONST_DEFINE_WEB_CATEGORY();
                        m_CONST_DEFINE_WEB_CATEGORY = m_CONST_DEFINE_WEB_CATEGORY_BLL.GetModel(Convert.ToInt32(strId));

                        txtID.Text = m_CONST_DEFINE_WEB_CATEGORY.ID.ToString();
                        txtCategoryName.Text = m_CONST_DEFINE_WEB_CATEGORY.CategoryName;
                        txtRemark.Text = m_CONST_DEFINE_WEB_CATEGORY.Remark;
                    }
                    else
                    {
                        gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请选择一条记录！");
                        gfAjaxJs.SetGoToUrl(this.UpdatePanel1, "CONST_DEFINE_WEB_CATEGORY_List.aspx");
                    }

                    if((strMode == "b")||(strMode == "r"))
                    {
                        txtID.ReadOnly = true;
                        txtID.CssClass = "csstextboxreadonly";
                        txtCategoryName.ReadOnly = true;
                        txtCategoryName.CssClass = "csstextboxreadonly";
                        txtRemark.ReadOnly = true;
                        txtRemark.CssClass = "csstextboxreadonly";
                        btnSave.Visible = false;
                    }

                    if(strMode == "r")
                    {
                        btnBack.Visible = false;
                    }
                }
                if (strMode == "i")
                {
                    lblOperateType.Text = "添加";
                }
                if (strMode == "b" || strMode == "r")
                {
                    lblOperateType.Text = "查看";
                }
                if (strMode == "u")
                {
                    lblOperateType.Text = "修改";
                }
            }

            btnBack.Attributes["onclick"] = "javascript:window.location.href='CONST_DEFINE_WEB_CATEGORY_List.aspx';return false;";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strID = txtID.Text.Trim();
            string strCategoryName = txtCategoryName.Text.Trim();
            string strRemark = txtRemark.Text.Trim();


            CONST_DEFINE_WEB_CATEGORY m_CONST_DEFINE_WEB_CATEGORY = new CONST_DEFINE_WEB_CATEGORY();
            CONST_DEFINE_WEB_CATEGORY_BLL m_CONST_DEFINE_WEB_CATEGORY_BLL = new CONST_DEFINE_WEB_CATEGORY_BLL();

            m_CONST_DEFINE_WEB_CATEGORY.CategoryName = strCategoryName;
            m_CONST_DEFINE_WEB_CATEGORY.Remark = strRemark;

            if (string.IsNullOrEmpty(strID) == true)
            {
                strID = "-1";
            }

            if (m_CONST_DEFINE_WEB_CATEGORY_BLL.Exists(Convert.ToInt32(strID)) == false)
            {
                m_CONST_DEFINE_WEB_CATEGORY.ID = m_CONST_DEFINE_WEB_CATEGORY_BLL.GetMax_ID();

                if (m_CONST_DEFINE_WEB_CATEGORY_BLL.Insert(m_CONST_DEFINE_WEB_CATEGORY) == true)
                {
                    gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "新增成功！");
                    txtID.Text = m_CONST_DEFINE_WEB_CATEGORY.ID.ToString();
                }
                else
                {
                    gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "新增失败！");
                }
            }
            else
            {
                if (m_CONST_DEFINE_WEB_CATEGORY_BLL.Update(m_CONST_DEFINE_WEB_CATEGORY, Convert.ToInt32(strID)) == true)
                {
                     gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "更新成功！");
                }
                else
                {
                    gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "更新失败！");
                }
            }
        }
    }
}
